﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ComputerGraphics.Graphics;
using ComputerGraphics.Utils;

namespace ComputerGraphics.Geometry
{
    public class Triangle : Plane
    {
        private Point3D a;
        private Point3D b;
        private Point3D c;

        private double area;

        public Triangle(Point3D a, Point3D b, Point3D c, Texture texture)
            : base(a, a - b, a - c, texture)
        {
            this.a = a;
            this.b = b;
            this.c = c;

            this.area = GeometryUtils.CalculateArea(a, b, c);
        }

        public override Intersection Intersect(Ray r)
        {
            Intersection ret = base.Intersect(r);

            if (ret != null)
            {
                double tempArea = GeometryUtils.CalculateArea(ret.Position, a, b) + GeometryUtils.CalculateArea(ret.Position, c, b) + GeometryUtils.CalculateArea(ret.Position, a, c);

                if (MathUtils.GetZeroDouble(tempArea - area) > 0)
                    ret = null;
            }

            return ret;
        }
    }
}
